package nl.hu.verbrandendephoenix.view;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.shared.Product;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SearchProduct {

	private Panel pnlSearchProduct, pnlPrice, pnlResult;
	private Grid grdSearchProductForm, grdResult;
	private ListBox lboCategory;
	private Label lblProductName, lblPrice, lblCategory, prodN;
	private TextBox txtProductName, txtPriceMin, txtPriceMax;
	private Button btnSearch, btnBack;
	private int prodID;

	public SearchProduct() {
		pnlSearchProduct = new VerticalPanel();
		pnlSearchProduct.addStyleName("post");
		pnlResult = new VerticalPanel();
	}

	public Panel constructForm() {
		grdSearchProductForm = new Grid(5, 3);

		Label title = new Label("Product zoeken:");
		title.addStyleName("h1");
		pnlSearchProduct.add(title);

		lblCategory = new Label("Categorie");

		grdSearchProductForm.setWidget(0, 0, lblCategory);

		lblProductName = new Label("Productnaam: ");
		txtProductName = new TextBox();

		grdSearchProductForm.setWidget(1, 0, lblProductName);
		grdSearchProductForm.setWidget(1, 1, txtProductName);

		pnlPrice = new HorizontalPanel();
		lblPrice = new Label("Prijs:");
		txtPriceMin = new TextBox();
		txtPriceMin.setWidth("50px");
		pnlPrice.add(txtPriceMin);
		pnlPrice.add(new Label("_"));
		txtPriceMax = new TextBox();
		txtPriceMax.setWidth("50px");
		pnlPrice.add(txtPriceMax);

		grdSearchProductForm.setWidget(2, 0, lblPrice);
		grdSearchProductForm.setWidget(2, 1, pnlPrice);

		btnSearch = new Button("Zoeken");

		grdSearchProductForm.setWidget(3, 1, btnSearch);

		pnlSearchProduct.add(grdSearchProductForm);

		return pnlSearchProduct;
	}

	public Panel constructResultForm(Grid result) {
		pnlResult.add(result);
		
		btnBack = new Button("Terug");
		pnlResult.add(btnBack);
		
		return pnlResult;
	}

	public Grid printResult(ArrayList<Product> result) {
		grdResult = new Grid(result.size() + 1, 5);
		grdResult.setWidget(0, 0, new HTML("<b>Productnaam</b>"));
		grdResult.setWidget(0, 1, new HTML("<b>Omschrijving</b>"));
		grdResult.setWidget(0, 2, new HTML("<b>Prijs</b>"));
		grdResult.setWidget(0, 3, new HTML("<b>Categorie</b>"));
		grdResult.setWidget(0, 4, new HTML("<b>Aangemaakt door</b>"));
		int rowCounter = 1;
		for(Product p : result) {
			prodN = new Label();
			setProdID(p.getProductID());
			prodN.setText(p.getProductName());
			grdResult.setWidget(rowCounter, 0, prodN);
			grdResult.setWidget(rowCounter, 1, new Label(p.getProductDescr()));
			grdResult.setWidget(rowCounter, 2, new Label(p.getPrice() + ""));
			grdResult.setWidget(rowCounter, 3, new Label(p.getCategory()));
			grdResult.setWidget(rowCounter, 4, new Label("USERNAMEPLACEHOLDER"));
			rowCounter++;
		}
		return grdResult;
	}

	public ArrayList<TextBox> getArrTextBox() {
		ArrayList<TextBox> arrTextBox = new ArrayList<TextBox>();
		arrTextBox.add(txtProductName);
		arrTextBox.add(txtPriceMin);
		arrTextBox.add(txtPriceMax);
		return arrTextBox;
	}

	public Button getBtnSearch() {
		return btnSearch;
	}

	public Button getBtnBack() {
		return btnBack;
	}

	public TextBox getTxtProductName() {
		return txtProductName;
	}

	public TextBox getTxtPriceMin() {
		return txtPriceMin;
	}

	public TextBox getTxtPriceMax() {
		return txtPriceMax;
	}

	public ListBox getLboCategory() {
		return lboCategory;
	}

	public String getLboCategorySelection() {
		return lboCategory.getValue(lboCategory.getSelectedIndex());
	}

	public void setLboCategory(String[] result) {
		lboCategory = new ListBox();
		lboCategory.addItem("Alles");
		for(int i = 0; i < result.length; i++) {
			lboCategory.addItem(result[i]);
		}
		grdSearchProductForm.setWidget(0, 1, lboCategory);
	}
	
	public Label getProdN() {
		return prodN;
	}
	
	public void setProdN(Label prod) {
		prodN = prod;
	}

	public int getProdID() {
		return prodID;
	}

	public void setProdID(int prodID) {
		this.prodID = prodID;
	}
}